package com.syqc.base

import android.os.Bundle
import android.os.PersistableBundle
import android.view.LayoutInflater
import androidx.viewbinding.ViewBinding
import com.amap.api.maps.AMap
import com.amap.api.maps.MapsInitializer
import com.syqc.comlib.base.BaseActivity
import com.syqc.monitor.databinding.IncludeMapMatchBinding

abstract class BaseMapActivity<VB : ViewBinding>(
    inflater: (LayoutInflater) -> VB
) : BaseActivity<VB>(inflater) {

    private lateinit var mapBindingVB: IncludeMapMatchBinding
    lateinit var mAMap: AMap

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mapBindingVB = IncludeMapMatchBinding.bind(binding.root)
        MapsInitializer.updatePrivacyShow(this, true, true)
        MapsInitializer.updatePrivacyAgree(this, true)
        mapBindingVB.mMapView.onCreate(savedInstanceState)
        mAMap = mapBindingVB.mMapView.map
        initMap()
    }

    abstract fun initMap()

    override fun onDestroy() {
        super.onDestroy()
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mAMap.clear()
        mapBindingVB.mMapView.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mapBindingVB.mMapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mapBindingVB.mMapView.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mapBindingVB.mMapView.onSaveInstanceState(outState)
    }


}